<template>
  <div class="xpage-detail">
  <nut-cell :title="state.canopyName" :desc="state.time" class="van-hairline--bottom"> </nut-cell>
  <div class="valve-operation">
    <nut-row>
      <nut-col :span="12" v-for="(child, index) in state.wxfkDeviceKeysList" :key="index">
        <div class="xblock-list">
          <div class="xblock-cell van-hairline--bottom">
            <div class="xblock-cell__title">
              <div>{{ child.devices?.name }}</div>
            </div>
            <span class="xblock-cell__switch" :class="getName(child.deviceRealData?.data) == '打开'?'active':''">{{ getName(child.deviceRealData?.data) }}</span>
          </div>
          <div class="xblock-content">
            <div class="flex-content-time">
              <span>{{ child.deviceRealData?.dataDate }}</span>
            </div>
            <div class="flex-content">
              <div class="switch-base" 
                v-if="getName(child.deviceRealData?.data) == '打开'"
                @click="openAndclose(index, child.devices?.serialNumber || '', '0')"
              >
                </div>
              <div class="switch-base disabled"
                v-else
                @click="openAndclose(index, child.devices?.serialNumber || '', '1')"
                ></div >
            </div>
      
          </div>
        </div>
      </nut-col>
    </nut-row>
  </div>
</div>
</template>

<script lang="ts" setup>
import Taro from '@tarojs/taro'
import { formatDate } from '@/common/util'
import { usePullDownRefresh, useReachBottom } from '@/hooks/life'
import { ref, onMounted, reactive } from 'vue'
import { useDidShow, useTabItemTap, useLoad } from '@tarojs/taro'
import { wxfkControlData } from '@/services/apis/history'
const state = reactive({
  canopyId: '',
  canopyName: '',
  wxfkDeviceKeysList: [],
  time: formatDate(Date.now(), 'Y-M-D')
})
useLoad(option => {
  state.canopyId = option.id
  state.canopyName = decodeURIComponent(option.canopyName)
  state.wxfkDeviceKeysList = Taro.getStorageSync('wxfkDeviceKeysList')
  console.log(Taro.getStorageSync('wxfkDeviceKeysList'))
})
const openAndclose = (index, serialNumber, onOff) => {
  wxfkControlData({ serialNumber, onOff }).then(data => {
    if (data.result == 'true' && data.message == '查询成功！') {
      state.wxfkDeviceKeysList[index].deviceRealData.data = onOff
    }
  })
}
const getName = type => {
  switch (type) {
    case '0':
      return '关闭'
      break
    case '1':
      return '打开'
      break
    case '2':
      return '活动中'
      break
    case '3':
      return '未安装'
      break
    case '4':
      return '暂停中'
      break
    case '5':
      return '阻止中'
      break
    case '6':
      return '回避'
      break
    default:
      return '关闭'
      break
  }
}
const getClass = type => {
  switch (type) {
    case '0':
      return 'yellow'
      break
    case '1':
      return 'green'
      break
    case '2':
      return ''
      break
    case '3':
      return ''
      break
    case '4':
      return ''
      break
    case '5':
      return 'red'
      break
    case '6':
      return ''
      break
    default:
      return ''
      break
  }
}
</script>

<style lang="scss">
@import '@/assets/styles/index.scss';
$border-radius: 6px;
$border-color: #eaeaea;
.valve-operation {
  padding: 0 5px;
}
.van-hairline--bottom {
  box-shadow: none;
}
.nut-cell {
  margin-top: 0;
  padding: 13px 10px;
  box-shadow: none;
  border-radius: 0;
  font-size: $font-size-3;
  color: #000;
}
.nut-cell__value {
  font-size: $font-size-0;
  color: #666;
}
.xblock-list {
  margin: 5px;
  padding: 0 10px;
  .xblock-cell__title {
    line-height: 24px;
    height: 24px;
  }
  .xblock-cell__switch{
    position: relative;
    padding-left: 10px;
    font-weight: 400;
    font-size: $font-size-0;
    color: #999999;
    &::before{
        content: '';
        position: absolute;
        top: 50%;
        left: 0;
        transform: translateY(-50%);
        width: 6px;
        height: 6px;
        border-radius: 50%;
        background: #999999;
    }
    &.active{
        color: #499D2F;
        &::before{
            background: #499D2F;
        }
    }
  }
  .xblock-content {
    padding: 8px 0 16px 0;
    overflow: hidden;
    font-size: $font-size-1;
    .flex-content-time{
        font-size: $font-size-0;
        color: #666;
    }
    .flex-content {
      display: flex;
      padding-top: 10px;
      .nut-button {
        border-radius: 5px;
      }
      .nut-button--success {
        & ~ span {
          color: $green-background;
        }
      }
      .nut-button--warning {
        & ~ span {
          color: #333;
        }
      }
    }
  }
}
.switch-base{
    position: relative;
    width: 42px;
    height: 24px;
    background: #499D2F;
    border-radius: 24px;
    overflow: hidden;
    &::before{
        content: '';
        position: absolute;
        top: 50%;
        right: 4px;
        transform: translateY(-50%);
        height: 18px;
        width: 18px;
        border-radius: 9px;
        background: #fff;
    }
    &::after{
        content: '';
        position: absolute;
        top: 50%;
        right: 12px;
        transform: translateY(-50%);
        height: 8px;
        width: 2px;
        border-radius: 1px;
        background: #499D2F;
    }
    &.disabled{
        background: #F1F2F9;
        &::before{
            left: 4px;
            right: auto;
        }
        &::after{
            height: 5px;
            width: 5px;
            left: 8px;
            border: 2px solid #499D2F;
            border-radius: 50%;
            background: #fff;
        }
    }
}
</style>
